<?php

abstract class BaseAction extends Base {
	
	protected $service;
	
	private $registrosporpagina;
	
	public function __construct($isSecure) {
		if( $isSecure ) {
			if( !SecurityManager::isAllowedAcess() ) {
				Logger::error($this,"Tentando acessar uma p�gina restrita");
				$this->go(Utils::getLink("/user/login"));
			}
		}
	}
	
	protected function setUserSession($obj) {
		Logger::debug($this,"Setando usuario em sess�o");
		Logger::debug($this,"Usuario : " . $obj->getLogin());
		Logger::debug($this,"Email : " . $obj->getEmail());
		SecurityManager::login($obj);
	}
	
	protected function getUserSession($obj) {
		return SecurityManager::getUser();
	}
	
	protected function validate($className, $fields = null, $guiFields = null) {
		$obj = new $className();
		if( $fields != null && $guiFields != null ) {
			for( $indice=0; $indice < sizeof($fields); $indice++ ) {
				$value = $this->get($fields[$indice]);	
				Logger::debug($this,"value = $value");
				if( $value == null || $value == "" ) {
					Logger::debug($this,"value � inv�lido");
					Util::alert("O campo " . $guiFields[$indice] . " � de preenchimento obrigat�rio");
					return false;
				}
			}		
		}
		return Util::fillObject($className);
	}
	
	protected function setService($service) {
		$this->service = $service;
	}
	
	protected function count() {
		return $this->service->count();
	}

	public function go($location) {
		Logger::debug($this,"Direcionando a request para a pagina $location");
		header("Location: " . $location);
	}
	
	public function alert($message) {
		echo Util::alert($message);
	}
	
	public function get($id) {
		return Scope::loadVar($id);	
	}
	
	public function set($id, $obj) {
		Scope::putPostVar($id, $obj);
	}
	
	public function addMessage($message, $messageType) {
		Logger::debug($this,"Adicionando a mensagem de erro '$message' no gestor de mensagens");
		
		$msg_arr;
		
		if( isset($_REQUEST[Constants::USER_MESSAGE]) ) {
			$msg_arr = &$_REQUEST[Constants::USER_MESSAGE];
		} else {
			$msg_arr = array();
		}
		
		$actionMessage = new ActionMessage();
		$actionMessage->setMessage($message);
		$actionMessage->setMessageType($messageType);
		
		$msg_arr[] = $actionMessage;
		
		$_REQUEST[Constants::USER_MESSAGE] = &$msg_arr;
	}	
}
?>